#include "mainwindow.h"
#include<QColorDialog>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
     setWindowTitle("图形绘制综合案例分析（双缓冲机制）");
      setMinimumSize(600,400);
    draw=new drawwidget(this);
    this->setCentralWidget(draw);
    bar=new QToolBar(this);
    linestyle=new QLabel(this);
    linestyle->setText(QString("线性风格"));
    com=new QComboBox(this);
    com->addItem("DashLine",2);
    com->addItem("SolidLine",1);
    linewidth=new QLabel("线宽");
    spin=new QSpinBox(this);
    spin->setRange(0,20);
    btn=new QToolButton(this);
    btn->setIcon(QIcon(":/color.png"));
    cleardraw=new QToolButton(this);
    cleardraw->setText("清除");
    bar->addWidget(linestyle);
     bar->addWidget(com);
      bar->addWidget(linewidth);
     bar->addWidget(spin);
    bar->addWidget(btn);
    bar->addWidget(cleardraw);
    this->addToolBar(bar);
    connect(com,QOverload<int>::of(&QComboBox::currentIndexChanged),this,& MainWindow::styleChange);
    connect(spin,QOverload<int>::of(&QSpinBox::valueChanged),this,& MainWindow::widthChange);
    connect(btn,&QToolButton::clicked,this,& MainWindow::colorChange);
    connect(cleardraw,&QToolButton::clicked,draw,&drawwidget::clearFun);
    draw->setStyle(2);

}

MainWindow::~MainWindow()
{
}

void MainWindow::styleChange()
{
    int sty=static_cast<int>(com->itemData(com->currentIndex(),Qt::UserRole).toInt());
    draw->setStyle(sty);
}

void MainWindow::widthChange()
{
    int wid=static_cast<int>(spin->value());
    draw->setWidth(wid);
}

void MainWindow::colorChange()
{
    QColor newcolor=QColorDialog::getColor(Qt::black, this, "选择颜色");
    if(newcolor.isValid())
       {
           draw->setColor(newcolor);
           QPixmap ps(20,20);
           ps.fill(newcolor);
       }


}

